package com.etrans.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.EmptyStackException;
import java.util.Iterator;

/* 
 * IDCard.java Created on 2006-5-24 17:03:37 
 * 
 */
public class CheckCID {
	
	final String area[] = {"11:北京","12:天津","13:河北","14:山西","15:内蒙古","21:辽宁","22:吉林","23:黑龙江","31:上海","32:江苏","33:浙江","34:安徽","35:福建","36:江西","37:山东","41:河南","42:湖北","43:湖南","44:广东","45:广西","46:海南","50:重庆","51:四川","52:贵州","53:云南","54:西藏","61:陕西","62:甘肃","63:青海","64:宁夏","65:新疆","71:台湾","81:香港","82:澳门","91:国外"};
	
	/*public static Collection getArea() {
		Collection coll = new ArrayList();
		coll.add(new LabelValueBean("11", "北京"));
		coll.add(new LabelValueBean("12", "天津"));
		coll.add(new LabelValueBean("13", "河北"));
		coll.add(new LabelValueBean("14", "山西"));
		coll.add(new LabelValueBean("15", "内蒙古"));
		coll.add(new LabelValueBean("21", "辽宁"));
		coll.add(new LabelValueBean("22", "吉林"));
		coll.add(new LabelValueBean("23", "黑龙江"));
		coll.add(new LabelValueBean("31", "上海"));
		coll.add(new LabelValueBean("32", "江苏"));
		coll.add(new LabelValueBean("33", "浙江"));
		coll.add(new LabelValueBean("34", "安徽"));
		coll.add(new LabelValueBean("35", "福建"));
		coll.add(new LabelValueBean("36", "江西"));
		coll.add(new LabelValueBean("37", "山东"));
		coll.add(new LabelValueBean("41", "河南"));
		coll.add(new LabelValueBean("42", "湖北"));
		coll.add(new LabelValueBean("43", "湖南"));
		coll.add(new LabelValueBean("44", "广东"));
		coll.add(new LabelValueBean("45", "广西"));
		coll.add(new LabelValueBean("46", "海南"));
		coll.add(new LabelValueBean("50", "重庆"));
		coll.add(new LabelValueBean("51", "四川"));
		coll.add(new LabelValueBean("52", "贵州"));
		coll.add(new LabelValueBean("53", "云南"));
		coll.add(new LabelValueBean("54", "西藏"));
		coll.add(new LabelValueBean("61", "陕西"));
		coll.add(new LabelValueBean("62", "甘肃"));
		coll.add(new LabelValueBean("63", "青海"));
		coll.add(new LabelValueBean("64", "宁夏"));
		coll.add(new LabelValueBean("65", "新疆"));
		coll.add(new LabelValueBean("71", "台湾"));
		coll.add(new LabelValueBean("81", "香港"));
		coll.add(new LabelValueBean("82", "澳门"));
		coll.add(new LabelValueBean("91", "国外"));
		return coll;
	}*/
	final int[] wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };

	final int[] vi = { 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 };

	private int[] ai = new int[18];

	/*public String[] getIDinfo(String idcard) {
		String[] idinfo = {"","",""};
		try {
			if (idcard.length() == 15) {
				idcard = uptoeighteen(idcard);
			}
			if (idcard.length() != 18) {
				return idinfo;
			}
			String sErea = getValue(getArea(),idcard.substring(0, 2));
			String verify = idcard.substring(17, 18).toUpperCase();
			String birth = idcard.substring(6, 10) + "-"+ idcard.substring(10, 12) + "-" + idcard.substring(12, 14);
			if (verify.equals(getVerify(idcard))) {
				if (Integer.parseInt(idcard.substring(16, 17)) % 2 == 1)
					idinfo = new String[]{sErea,birth,"男"};
				else
					idinfo = new String[]{sErea,birth,"女"};
			}
			return idinfo;
		} catch (EmptyStackException e) {
			return idinfo;
		}
	}
*/
	public boolean verify(String idcard) {
		try {
			if (idcard.length() == 15) {
				idcard = uptoeighteen(idcard);
			}
			if (idcard.length() != 18) {
				return false;
			}
			String verify = idcard.substring(17, 18).toUpperCase();
			String birth = idcard.substring(6, 10) + "-"
					+ idcard.substring(10, 12) + "-" + idcard.substring(12, 14);
			if (verify.equals(getVerify(idcard)))
				return true;
			return false;
		} catch (EmptyStackException e) {
			return false;
		}
	}

	public boolean verifySex(String idcard, String sex) {
		try {
			if (idcard.length() == 15) {
				idcard = uptoeighteen(idcard);
			}
			if (idcard.length() != 18) {
				return false;
			}
			String verify = idcard.substring(17, 18).toUpperCase();
			String flagSex = "";
			if (verify.equals(getVerify(idcard))) {
				if (Integer.parseInt(idcard.substring(16, 17)) % 2 == 1)
					flagSex="M";
				else
					flagSex="F";
			}
			if(flagSex.equals(trim(sex).toUpperCase()))
				return true;
			else
				return false;
		} catch (EmptyStackException e) {
			return false;
		}
	}
	public boolean verifyBirthday(String idcard, String birthday) {
		try {
			if (idcard.length() == 15) {
				idcard = uptoeighteen(idcard);
			}
			if (idcard.length() != 18) {
				return false;
			}
			String birth = idcard.substring(6, 10) + "-"+ idcard.substring(10, 12) + "-" + idcard.substring(12, 14);
			
			if(birthday.equals(birth))
				return true;
			else
				return false;
		} catch (EmptyStackException e) {
			return false;
		}
	}
	
	public String getBirthday(String idcard) {
		String birth ="";
		try {
			if (idcard.length() == 15) {
				idcard = uptoeighteen(idcard);
			}
			if (idcard.length() != 18) {
				return "";
			}
			birth = idcard.substring(6, 10) + "-"+ idcard.substring(10, 12) + "-" + idcard.substring(12, 14);
			
		} catch (EmptyStackException e) {
			return "";
		}
		return birth; 
	}
	public String getSex(String idcard) {
		String flagSex = "";
		try {
			if (idcard.length() == 15) {
				idcard = uptoeighteen(idcard);
			}
			if (idcard.length() != 18) {
				return "";
			}
			String verify = idcard.substring(17, 18).toUpperCase();
			if (verify.equals(getVerify(idcard))) {
				if (Integer.parseInt(idcard.substring(16, 17)) % 2 == 1)
					flagSex="M";
				else
					flagSex="F";
			}
		} catch (EmptyStackException e) {
			return "";
		}
		return flagSex;
	}
	// get verify
	private String getVerify(String eightcardid) {
		int remaining = 0;
		if (eightcardid.length() == 18) {
			eightcardid = eightcardid.substring(0, 17);
		}
		if (eightcardid.length() == 17) {
			int sum = 0;
			for (int i = 0; i < 17; i++) {
				String k = eightcardid.substring(i, i + 1);
				ai[i] = Integer.parseInt(k);
			}
			for (int i = 0; i < 17; i++) {
				sum = sum + wi[i] * ai[i];
			}
			remaining = sum % 11;
		}
		String n =  String.valueOf(vi[remaining]);
		return remaining == 2 ? "X" : n;
		
	}

	// 15 update to 18
	private String uptoeighteen(String fifteencardid) {
		String eightcardid = fifteencardid.substring(0, 6);
		eightcardid = eightcardid + "19";
		eightcardid = eightcardid + fifteencardid.substring(6, 15);
		eightcardid = eightcardid + getVerify(eightcardid);
		return eightcardid;
	}
/*	public static String getValue(Object coll, String lable) {
		Collection myColl = (Collection) coll;
		if (myColl == null)
			return "";
		Iterator it = myColl.iterator();
		while (it.hasNext()) {
			LabelValueBean bean = (LabelValueBean) it.next();
			if (bean.getLabel().equals(lable)) {
				return bean.getValue();
			}
		}
		return "";
	}*/
	
	public static String trim(String str) {
		str = toEmpty(str).trim();
		return str;
	}
	
	public static String toEmpty(String str) {
		return (str == null ? "" : str);
	}
	
	public static void main(String[] args){
	    CheckCID test = new CheckCID();
	    System.out.println(test.verify("232103198303144874"));
	}
}